const app = getApp()

import axios from '../../utils/axios'

Page({

  data: {
    province: '', // 省市区
    postcode: '', // 邮政编码
    details: '', // 详细地址
    userPhone: '', // 手机号
    userName: '', // 姓名
    contactId: '', // 如果是编辑，那么此id就有值
  },

  onLoad: function (options) {
    this.setData({
      contactId: options.contactId
    })

    if (this.data.contactId) {
      wx.setNavigationBarTitle({
        title: '修改地址',
      });
      this.contactDetail()
    } else {
      wx.setNavigationBarTitle({
        title: '新增地址',
      });
    }

  },

  onShow: function () {

  },

  // 根据联系人id查询信息
  contactDetail: function () {
    axios('/personal/contactDetail', {
      contact_id: this.data.contactId
    }).then(res => {
      if (res.data.resultcode == 1) {
        let data = res.data.result;
        this.setData({
          userName: data.contact,
          userPhone: data.phone,
          province: data.address,
          details: data.detail_address,
          postcode: data.postal_code
        })
      }
    })
  },

  // 保存地址
  saveAddress: function () {

    if (this.data.userName == '') {
      wx.showToast({
        title: '请输入联系姓名！',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (this.data.userPhone.length != 11) {
      wx.showToast({
        title: '手机号码不正确',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (this.data.province == '') {
      wx.showToast({
        title: '请选择地区！',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (this.data.detail == '') {
      wx.showToast({
        title: '请输入详细地址！',
        icon: 'none',
        duration: 1500
      })
      return
    }

    let requestPath = "/personal/appendContact"

    let paramJson = {
      contact: this.data.userName, //联系人
      phone: this.data.userPhone,
      address: this.data.province, //地址
      detail_address: this.data.details, //
      store_id: app.globalData.storeId,
      postal_code: this.data.postcode
    }

    if (this.data.contactId) {
      requestPath = "/personal/modifyContact"
      paramJson['contact_id'] = this.data.contactId
    }

    axios(requestPath, paramJson).then(res => {
      console.log(res)
      if (res.data.resultcode == 1) {
        wx.showToast({
          title: '保存成功',
          icon: 'none',
          duration: 1300
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1300)
      }
    })

  },

  // 获取微信地址
  getAddress: function () {
    const self = this
    wx.chooseAddress({
      success(res) {
        console.log(res)
        self.setData({
          userName: res.userName,
          userPhone: res.telNumber,
          province: res.provinceName + res.cityName + res.countyName,
          details: res.detailInfo,
          postcode: res.postalCode
        })
      },
      fail(err) {
        console.log(err)
        wx.showModal({
          title: '提示',
          content: '是否去设置打开授权？',
          success(res) {
            console.log(res)
            if (res.confirm) {
              wx.openSetting({
                success(result) {
                  if (result.authSetting['scope.address']) {
                    wx.chooseAddress({
                      success(data) {
                        self.setData({
                          userName: data.userName,
                          userPhone: data.telNumber,
                          province: data.provinceName + data.cityName + data.countyName,
                          details: data.detailInfo,
                          postcode: data.postalCode
                        })
                      },
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  },

  // 输入姓名
  userName: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },

  // 输入手机号
  userPhone: function (e) {
    this.setData({
      userPhone: e.detail.value
    })
  },

  // 输入详细地址
  details: function (e) {
    this.setData({
      details: e.detail.value
    })
  },

  // 选择省市区
  selectProvince: function (e) {

    console.log(e)

    let province = ""
    let postcode = e.detail.postcode

    for (let i = 0; i < e.detail.value.length; i++) {
      province += e.detail.value[i]
    }

    this.setData({
      province: province,
      postcode: postcode
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})